//
//  EmojiView.m
//  MMHelp
//
//  Created by kzzang on 13-2-17.
//
//

#import "EmojiView.h"
#import "ConstantHeader.h"
#import "Tool.h"

@implementation EmojiView

#pragma mark - Init

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    }
    return self;
}

#pragma mark - Interface Method
#define kOnePageDisplayCount 24
#define kEmojiButtonWidth 28.0f
- (void)displayEmojiWithContents:(NSArray *) contents {
    
    for (UIButton *emojiButton in [self subviews]) {
        [emojiButton removeFromSuperview];
    }
    
    int startCol = 0;
    int startRow = 0;
    for (int i = 1; i<= kOnePageDisplayCount; i++) {
        
        // 设置表情显示区域
        CGRect viewRect = CGRectMake(UF(56) * startCol + (UF(24) * startCol) + UF(10), startRow * kEmojiButtonWidth + (12.0f * startRow), UF(56), UF(56));
        
        // 设置按钮
        UIButton *emojiButton = [UIButton buttonWithType:UIButtonTypeCustom];
        //emojiButton.backgroundColor = [UIColor whiteColor];
        emojiButton.frame = viewRect;
        // Memory issue
        [emojiButton.titleLabel setFont:[UIFont fontWithName:@"Apple Color Emoji" size:kEmojiButtonWidth]];
        [emojiButton setTitleEdgeInsets:UIEdgeInsetsMake(7, 0, 0, 0)];
        NSString *emojiString = getSafeObjectAtIndex(i-1,contents);
        [emojiButton setTitle:emojiString forState:UIControlStateNormal];
        [emojiButton addTarget:self.lmbangEmojiView action:@selector(emojiButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:emojiButton];
        /*
         Emoji  *emoji = [[Emoji alloc] initWithFrame:viewRect];
         emoji.delegate = self;
         int resourceIndex = ((currentPage - 1) * kOnePageDisplayCount) + (i - 1);
         NSString *emojiString = [self.emojis objectAtIndex:resourceIndex];
         [emoji setEmojiString:emojiString];
         [view addSubview:emoji];
         [emoji release];
         */
        startCol++;
        if (i%8 == 0) {
            startCol = 0;
            startRow++;
        }
    }

}


@end
